package com.ymatou.iapi.deviceid.testcase;

import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.deviceid.parameter.SaveBean;
import com.ymatou.iapi.deviceid.parameter.UpdateBean;
import com.ymatou.iapi.deviceid.service.UpdateCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.DeviceidCallService;
import com.ymttest.database.mongo.DeviceidBean;
import com.ymttest.database.mongo.DeviceidWapper;
import com.ymttest.utils.TestDataManager;
/**
*更新DeviceId信息接口用例
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_Update{
	private static UpdateBean updateBean;
	private static UpdateCall updateCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("更新DeviceId信息接口用例");
	}

	@Before
	public void caseUp() {
		updateBean=new UpdateBean();
		updateCall=new UpdateCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(updateCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_Update_001() {
		Logger.start(false,"校验deviceid，如果为空返回 code=100");
		try {
			
			updateBean.setUserid(1234);
			updateBean.setDid("12333");
			
			updateCall.setData(updateBean);
			updateCall.callService();
			
			Logger.verifyEquals("100", updateCall.getCode(), "验证返回Code");
			Logger.verifyEquals("0", updateCall.getBCode(), "验证返回BCode");
			Logger.verifyEquals("DeviceId不能为空.", updateCall.getMSG(), "验证返回msg");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_Update_002() {
		Logger.start(true,"根据deviceid获取最新的一条记录，如果记录为空则新增，signVerified = 1。");
		try {
			
			String deviceid = "Tc_Update"+TestDataManager.getRandomNum(10);
			Integer userid=Integer.parseInt(TestDataManager.getRandomNum(5));
			String did = "Tc_Update_Did"+TestDataManager.getRandomNum(10);
			
			updateBean.setDeviceid(deviceid);
			updateBean.setUserid(userid);
			updateBean.setDid(did);
			
			updateCall.setData(updateBean);
			updateCall.callService();
			
			Logger.verifyEquals("0", updateCall.getCode(), "验证返回Code");
			Logger.verifyEquals("0", updateCall.getBCode(), "验证返回BCode");
			Logger.verifyEquals("null", updateCall.getMSG(), "验证返回msg");
			
			List<DeviceidBean> deviceidBeans = new DeviceidWapper().getByDeviceid(deviceid);

			Logger.verifyEquals(1, deviceidBeans.size(), "验证Monggo库生成一条DeviceId记录。");
			
			if(deviceidBeans.size()==1)
			{
				DeviceidBean bean = deviceidBeans.get(0);
				Logger.verifyEquals(deviceid, bean.getDeviceid(), "验证数据库Deviceid(保存正确");
				Logger.verifyEquals(userid, bean.getUserid(), "验证数据库Userid保存正确");
				Logger.verifyEquals(did, bean.getDid(), "验证数据库Did保存正确");
				Logger.verifyEquals(1, bean.getSignVerified(), "验证数据库SignVerified保存正确");

				Logger.verifyNotNull(bean.getUpdateTime(), "验证数据库UpdateTime保存正确");	
				Logger.verifyNotNull(bean.getActiveTime(), "验证数据库ActiveTime保存正确");				
			}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_Update_003() {
		Logger.start(true,"如果存在记录，并且记录中的userid == 0，则用请求体中的userid更新记录。");
		try {
			
			SaveBean saveBean = DeviceidCallService.setDefaultFullSaveBean();
			saveBean.setUserid(0);
			DeviceidCallService.saveCall(saveBean);
			
			String deviceid = saveBean.getDeviceid();
			Integer userid=Integer.parseInt(TestDataManager.getRandomNum(5));
			String did = "Tc_Update_Did"+TestDataManager.getRandomNum(10);
			
			List<DeviceidBean> deviceidBeans1 = new DeviceidWapper().getByDeviceid(deviceid);
			Logger.verifyEquals(1, deviceidBeans1.size(), "验证Monggo库生成一条DeviceId记录。");	
			
			updateBean.setDeviceid(deviceid);
			updateBean.setUserid(userid);
			updateBean.setDid(did);
			
			updateCall.setData(updateBean);
			updateCall.callService();
			
			Logger.verifyEquals("0", updateCall.getCode(), "验证返回Code");
			Logger.verifyEquals("0", updateCall.getBCode(), "验证返回BCode");
			Logger.verifyEquals("null", updateCall.getMSG(), "验证返回msg");
			
			List<DeviceidBean> deviceidBeans2 = new DeviceidWapper().getByDeviceid(deviceid);
			Logger.verifyEquals(1, deviceidBeans2.size(), "验证Monggo库生成一条DeviceId记录。");
			
			if(deviceidBeans2.size()==1)
			{
				DeviceidBean bean = deviceidBeans2.get(0);
				Logger.verifyEquals(saveBean.getAppid(), bean.getAppid(), "验证数据库Appid保存正确");
				Logger.verifyEquals(saveBean.getDeviceid(), bean.getDeviceid(), "验证数据库Deviceid(保存正确");
				Logger.verifyEquals(saveBean.getClienttype(), bean.getClienttype(), "验证数据库Clienttype保存正确");
				Logger.verifyEquals(saveBean.getAppversion(), bean.getAppversion(), "验证数据库Appversion保存正确");
				Logger.verifyEquals(saveBean.getPackageid(), bean.getPackageid(), "验证数据库Packageid保存正确");
				Logger.verifyEquals(saveBean.getIp(), bean.getIp(), "验证数据库Ip保存正确");
				Logger.verifyEquals(userid, bean.getUserid(), "验证数据库Userid保存正确");
				Logger.verifyEquals(saveBean.getCcid(), bean.getCcid(), "验证数据库Ccid保存正确");
				Logger.verifyEquals(saveBean.getCkid(), bean.getCkid(), "验证数据库Ckid保存正确");
				Logger.verifyEquals(saveBean.getOsversion(), bean.getOsversion(), "验证数据库Osversion保存正确");
				
				//sign字段不入库
//				Logger.verifyEquals(saveBean.getSign(), bean.getSign(), "验证数据库Sign保存正确");
				Logger.verifyEquals(saveBean.getNetwork(), bean.getNetwork(), "验证数据库Network保存正确");
				Logger.verifyEquals(saveBean.getYid(), bean.getYid(), "验证数据库Yid保存正确");
				Logger.verifyEquals(saveBean.getDid(), bean.getDid(), "验证数据库Did保存正确");
				Logger.verifyEquals(saveBean.getIsupdate(), bean.getIsupdate(), "验证数据库Isupdate保存正确");
				
				Logger.verifyEquals(saveBean.getPower(), bean.getPower(), "验证数据库Power保存正确");
				Logger.verifyEquals(saveBean.getAppkey(), bean.getAppkey(), "验证数据库Appkey保存正确");
				Logger.verifyEquals(saveBean.getTimestamp(), bean.getTimestamp(), "验证数据库Timestamp保存正确");
				Logger.verifyEquals(saveBean.getAcceptversion(), bean.getAcceptversion(), "验证数据库Acceptversion保存正确");
				Logger.verifyEquals(saveBean.getMac(), bean.getMac(), "验证数据库Mac保存正确");
				Logger.verifyEquals(saveBean.getSignVerified(), bean.getSignVerified(), "验证数据库SignVerified保存正确");

				Logger.verifyNotNull(bean.getUpdateTime(), "验证数据库UpdateTime保存正确");
				
			}
		
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_Update_004() {
		Logger.start(true,"如果存在记录，并且记录中的userid ==  请求体中的userid，不做任何处理直接返回。");
		try {
			
			SaveBean saveBean = DeviceidCallService.setDefaultFullSaveBean();
			DeviceidCallService.saveCall(saveBean);
			
			String deviceid = saveBean.getDeviceid();
			Integer userid=saveBean.getUserid();
			String did = saveBean.getDid();
			
			List<DeviceidBean> deviceidBeans1 = new DeviceidWapper().getByDeviceid(deviceid);
			Logger.verifyEquals(1, deviceidBeans1.size(), "验证Monggo库生成一条DeviceId记录。");	
			
			updateBean.setDeviceid(deviceid);
			updateBean.setUserid(userid);
			updateBean.setDid(did);
			
			updateCall.setData(updateBean);
			updateCall.callService();
			
			Logger.verifyEquals("0", updateCall.getCode(), "验证返回Code");
			Logger.verifyEquals("0", updateCall.getBCode(), "验证返回BCode");
			Logger.verifyEquals("null", updateCall.getMSG(), "验证返回msg");
			
			List<DeviceidBean> deviceidBeans2 = new DeviceidWapper().getByDeviceid(deviceid);
			Logger.verifyEquals(1, deviceidBeans2.size(), "验证Monggo库生成一条DeviceId记录。");
		
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_Update_005() {
		Logger.start(true,"如果存在记录，并且记录中的userid != 请求体中的userid，DDid传值，则使用已存在记录新增一条DeviceId，UserId赋新值Updatetime=now，DDid为新值。-JIRACONN-4789");
		try {
			
			SaveBean saveBean = DeviceidCallService.setDefaultFullSaveBean();
			DeviceidCallService.saveCall(saveBean);
			
			String deviceid = saveBean.getDeviceid();
			Integer userid=Integer.parseInt(TestDataManager.getRandomNum(5));
			String did = "Tc_Update_Did"+TestDataManager.getRandomNum(10);
			
			List<DeviceidBean> deviceidBeans1 = new DeviceidWapper().getByDeviceid(deviceid);
			Logger.verifyEquals(1, deviceidBeans1.size(), "验证Monggo库生成一条DeviceId记录。");	
			
			updateBean.setDeviceid(deviceid);
			updateBean.setUserid(userid);
			updateBean.setDid(did);
			
			updateCall.setData(updateBean);
			updateCall.callService();
			
			Logger.verifyEquals("0", updateCall.getCode(), "验证返回Code");
			Logger.verifyEquals("0", updateCall.getBCode(), "验证返回BCode");
			Logger.verifyEquals("null", updateCall.getMSG(), "验证返回msg");
			
			List<DeviceidBean> deviceidBeans2 = new DeviceidWapper().getByDeviceid(deviceid);
			Logger.verifyEquals(2, deviceidBeans2.size(), "验证Monggo库生成一条DeviceId记录。");		

			if(deviceidBeans2.size()==2)
			{
				DeviceidBean bean = deviceidBeans2.get(1);
				Logger.verifyEquals(saveBean.getAppid(), bean.getAppid(), "验证数据库Appid保存正确");
				Logger.verifyEquals(saveBean.getDeviceid(), bean.getDeviceid(), "验证数据库Deviceid(保存正确");
				Logger.verifyEquals(saveBean.getClienttype(), bean.getClienttype(), "验证数据库Clienttype保存正确");
				Logger.verifyEquals(saveBean.getAppversion(), bean.getAppversion(), "验证数据库Appversion保存正确");
				Logger.verifyEquals(saveBean.getPackageid(), bean.getPackageid(), "验证数据库Packageid保存正确");
				Logger.verifyEquals(saveBean.getIp(), bean.getIp(), "验证数据库Ip保存正确");
				Logger.verifyEquals(userid, bean.getUserid(), "验证数据库Userid保存正确");
				Logger.verifyEquals(saveBean.getCcid(), bean.getCcid(), "验证数据库Ccid保存正确");
				Logger.verifyEquals(saveBean.getCkid(), bean.getCkid(), "验证数据库Ckid保存正确");
				Logger.verifyEquals(saveBean.getOsversion(), bean.getOsversion(), "验证数据库Osversion保存正确");
				
				//sign字段不入库
//				Logger.verifyEquals(saveBean.getSign(), bean.getSign(), "验证数据库Sign保存正确");
				Logger.verifyEquals(saveBean.getNetwork(), bean.getNetwork(), "验证数据库Network保存正确");
				Logger.verifyEquals(saveBean.getYid(), bean.getYid(), "验证数据库Yid保存正确");
				Logger.verifyEquals(did, bean.getDid(), "验证数据库Did保存正确");
				Logger.verifyEquals(saveBean.getIsupdate(), bean.getIsupdate(), "验证数据库Isupdate保存正确");
				
				Logger.verifyEquals(saveBean.getPower(), bean.getPower(), "验证数据库Power保存正确");
				Logger.verifyEquals(saveBean.getAppkey(), bean.getAppkey(), "验证数据库Appkey保存正确");
				Logger.verifyEquals(saveBean.getTimestamp(), bean.getTimestamp(), "验证数据库Timestamp保存正确");
				Logger.verifyEquals(saveBean.getAcceptversion(), bean.getAcceptversion(), "验证数据库Acceptversion保存正确");
				Logger.verifyEquals(saveBean.getMac(), bean.getMac(), "验证数据库Mac保存正确");
				Logger.verifyNotNull(bean.getUpdateTime(), "验证数据库UpdateTime保存正确");
				
				//JIRACONN-4789
				Logger.verifyEquals(saveBean.getSignVerified(), bean.getSignVerified(), "验证数据库SignVerified保存正确");
			}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_Update_006() {
		Logger.start(true,"如果存在记录，并且记录中的userid != 请求体中的userid，DDid不传值，则使用已存在记录新增一条DeviceId，DDid为null值。");
		try {
			
			SaveBean saveBean = DeviceidCallService.setDefaultFullSaveBean();
			DeviceidCallService.saveCall(saveBean);
			
			String deviceid = saveBean.getDeviceid();
			Integer userid=Integer.parseInt(TestDataManager.getRandomNum(5));
			
			List<DeviceidBean> deviceidBeans1 = new DeviceidWapper().getByDeviceid(deviceid);
			Logger.verifyEquals(1, deviceidBeans1.size(), "验证Monggo库生成一条DeviceId记录。");	
			
			updateBean.setDeviceid(deviceid);
			updateBean.setUserid(userid);
			
			updateCall.setData(updateBean);
			updateCall.callService();
			
			Logger.verifyEquals("0", updateCall.getCode(), "验证返回Code");
			Logger.verifyEquals("0", updateCall.getBCode(), "验证返回BCode");
			Logger.verifyEquals("null", updateCall.getMSG(), "验证返回msg");
			
			List<DeviceidBean> deviceidBeans2 = new DeviceidWapper().getByDeviceid(deviceid);
			Logger.verifyEquals(2, deviceidBeans2.size(), "验证Monggo库生成一条DeviceId记录。");		

			if(deviceidBeans2.size()==2)
			{
				DeviceidBean bean = deviceidBeans2.get(1);
				Logger.verifyEquals(saveBean.getAppid(), bean.getAppid(), "验证数据库Appid保存正确");
				Logger.verifyEquals(saveBean.getDeviceid(), bean.getDeviceid(), "验证数据库Deviceid(保存正确");
				Logger.verifyEquals(saveBean.getClienttype(), bean.getClienttype(), "验证数据库Clienttype保存正确");
				Logger.verifyEquals(saveBean.getAppversion(), bean.getAppversion(), "验证数据库Appversion保存正确");
				Logger.verifyEquals(saveBean.getPackageid(), bean.getPackageid(), "验证数据库Packageid保存正确");
				Logger.verifyEquals(saveBean.getIp(), bean.getIp(), "验证数据库Ip保存正确");
				Logger.verifyEquals(userid, bean.getUserid(), "验证数据库Userid保存正确");
				Logger.verifyEquals(saveBean.getCcid(), bean.getCcid(), "验证数据库Ccid保存正确");
				Logger.verifyEquals(saveBean.getCkid(), bean.getCkid(), "验证数据库Ckid保存正确");
				Logger.verifyEquals(saveBean.getOsversion(), bean.getOsversion(), "验证数据库Osversion保存正确");
				
				//sign字段不入库
//				Logger.verifyEquals(saveBean.getSign(), bean.getSign(), "验证数据库Sign保存正确");
				Logger.verifyEquals(saveBean.getNetwork(), bean.getNetwork(), "验证数据库Network保存正确");
				Logger.verifyEquals(saveBean.getYid(), bean.getYid(), "验证数据库Yid保存正确");
				Logger.verifyEquals(null, bean.getDid(), "验证数据库Did保存正确");
				Logger.verifyEquals(saveBean.getIsupdate(), bean.getIsupdate(), "验证数据库Isupdate保存正确");
				
				Logger.verifyEquals(saveBean.getPower(), bean.getPower(), "验证数据库Power保存正确");
				Logger.verifyEquals(saveBean.getAppkey(), bean.getAppkey(), "验证数据库Appkey保存正确");
				Logger.verifyEquals(saveBean.getTimestamp(), bean.getTimestamp(), "验证数据库Timestamp保存正确");
				Logger.verifyEquals(saveBean.getAcceptversion(), bean.getAcceptversion(), "验证数据库Acceptversion保存正确");
				Logger.verifyEquals(saveBean.getMac(), bean.getMac(), "验证数据库Mac保存正确");
				//JIRACONN-4789
				Logger.verifyEquals(saveBean.getSignVerified(), bean.getSignVerified(), "验证数据库SignVerified保存正确");
			}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_Update_007() {
		Logger.start(true,"userid 空");
		try {

			updateBean.setDeviceid("12344");
			
			updateCall.setData(updateBean);
			updateCall.callService();
			
			Logger.verifyEquals("0", updateCall.getCode(), "验证返回Code");
			Logger.verifyEquals("0", updateCall.getBCode(), "验证返回BCode");
			Logger.verifyEquals("null", updateCall.getMSG(), "验证返回msg");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_Update_008() {
		Logger.start(true,"JIRACONN-17427存在记录的machinename不会更新");
		try {
			
			SaveBean saveBean = DeviceidCallService.setDefaultFullSaveBean();
			saveBean.setMachinename("iphoneXX");
			saveBean.setUserid(0);
			DeviceidCallService.saveCall(saveBean);
			
			String deviceid = saveBean.getDeviceid();
			Integer userid=Integer.parseInt(TestDataManager.getRandomNum(5));
			String did = "Tc_Update_Did"+TestDataManager.getRandomNum(10);
			
			List<DeviceidBean> deviceidBeans1 = new DeviceidWapper().getByDeviceid(deviceid);
			Logger.verifyEquals(1, deviceidBeans1.size(), "验证Monggo库生成一条DeviceId记录。");	
			
			updateBean.setDeviceid(deviceid);
			updateBean.setUserid(userid);
			updateBean.setDid(did);
			
			updateCall.setData(updateBean);
			updateCall.callService();
			
			Logger.verifyEquals("0", updateCall.getCode(), "验证返回Code");
			Logger.verifyEquals("0", updateCall.getBCode(), "验证返回BCode");
			Logger.verifyEquals("null", updateCall.getMSG(), "验证返回msg");
			
			List<DeviceidBean> deviceidBeans2 = new DeviceidWapper().getByDeviceid(deviceid);
			Logger.verifyEquals(1, deviceidBeans2.size(), "验证Monggo库生成一条DeviceId记录。");
			
			if(deviceidBeans2.size()==1)
			{
				DeviceidBean bean = deviceidBeans2.get(0);
				Logger.verifyEquals(saveBean.getAppid(), bean.getAppid(), "验证数据库Appid保存正确");
				Logger.verifyEquals(saveBean.getDeviceid(), bean.getDeviceid(), "验证数据库Deviceid(保存正确");
				Logger.verifyEquals(saveBean.getClienttype(), bean.getClienttype(), "验证数据库Clienttype保存正确");
				Logger.verifyEquals(saveBean.getAppversion(), bean.getAppversion(), "验证数据库Appversion保存正确");
				Logger.verifyEquals(saveBean.getPackageid(), bean.getPackageid(), "验证数据库Packageid保存正确");
				Logger.verifyEquals(saveBean.getIp(), bean.getIp(), "验证数据库Ip保存正确");
				Logger.verifyEquals(userid, bean.getUserid(), "验证数据库Userid保存正确");
				Logger.verifyEquals(saveBean.getCcid(), bean.getCcid(), "验证数据库Ccid保存正确");
				Logger.verifyEquals(saveBean.getCkid(), bean.getCkid(), "验证数据库Ckid保存正确");
				Logger.verifyEquals(saveBean.getOsversion(), bean.getOsversion(), "验证数据库Osversion保存正确");
				
				//sign字段不入库
//				Logger.verifyEquals(saveBean.getSign(), bean.getSign(), "验证数据库Sign保存正确");
				Logger.verifyEquals(saveBean.getNetwork(), bean.getNetwork(), "验证数据库Network保存正确");
				Logger.verifyEquals(saveBean.getYid(), bean.getYid(), "验证数据库Yid保存正确");
				Logger.verifyEquals(saveBean.getDid(), bean.getDid(), "验证数据库Did保存正确");
				Logger.verifyEquals(saveBean.getIsupdate(), bean.getIsupdate(), "验证数据库Isupdate保存正确");
				
				Logger.verifyEquals(saveBean.getPower(), bean.getPower(), "验证数据库Power保存正确");
				Logger.verifyEquals(saveBean.getAppkey(), bean.getAppkey(), "验证数据库Appkey保存正确");
				Logger.verifyEquals(saveBean.getTimestamp(), bean.getTimestamp(), "验证数据库Timestamp保存正确");
				Logger.verifyEquals(saveBean.getAcceptversion(), bean.getAcceptversion(), "验证数据库Acceptversion保存正确");
				Logger.verifyEquals(saveBean.getMac(), bean.getMac(), "验证数据库Mac保存正确");
				Logger.verifyEquals(saveBean.getSignVerified(), bean.getSignVerified(), "验证数据库SignVerified保存正确");
				Logger.verifyEquals(saveBean.getMachinename(), bean.getMachinename(), "验证数据库Machinename保存正确");
				Logger.verifyNotNull(bean.getUpdateTime(), "验证数据库UpdateTime保存正确");
				
			}
		
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}